package net.tailfish.starter.common.generator;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

/**
 * @PackageName net.tailfish.starter.common.generator
 * @ClassName IdGeneratorUtils
 * @Author tailFish
 * @Date Created in 11:06 2021/7/26
 * @Description ID生成器
 */
public class IdGeneratorUtils {

    private Snowflake snowflake;

    public IdGeneratorUtils(Snowflake snowflake){
        this.snowflake = snowflake;
    }

    /**
     * 获取ID
     * @return {{String.class}}
     */
    public String getNextId(){
        String nextIdStr = snowflake.nextIdStr().concat(DateTimeFormatter.ofPattern("yyyyMMddHH").format(LocalDateTime.now())).concat(randomNumeric(4));
        return nextIdStr.length() > 32 ? nextIdStr.substring(0, 32) : nextIdStr;
    }


    /**
     * 生成的UUID是带-的字符串，类似于：a5c8a5e8-df2b-4706-bea4-08d0939410e3
     * @return {{uuid}}
     */
    public String getUUID(){
        return IdUtil.randomUUID();
    }


    /**
     * 生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
     * @return {{uuid}}
     */
    public String getSimpleUUID(){
        return IdUtil.simpleUUID();
    }


}
